 @include "query.awk"

 function findSome(query,found,at,active,   init) { 
   array(init) 
   findSome_and(query,at,init,found,active,1)
   return length(found)
 }
 function findSome_and(query,at,before,found,active,nth,   col,now,new) {
   for(col in query) {
     ats(col,query[col],at,now,active)
     delete query[col]
     if (nth==1)
       return findSome_and(query,at,now,found,active, ++nth)
     else {
       findSome_intersect(before,now, new)
       return findSome_and(query,at,new,found,active, ++nth) 
   }}
   return asort(before,found,"valasc")
 }
 function findSome_intersect(x,y,new,   i,n,tmp1,tmp2,id) {
   array(new)
   for(i in x) { id[x[i]["id"]]; tmp1[x[i]["id"]] = x[i]["val"]}
   for(i in y) { id[y[i]["id"]]; tmp2[y[i]["id"]] = y[i]["val"]}
   for(i in id) {
     n++ 
     new[n]["id"]=i ;
     new[n]["val"]= tmp1[i]+tmp2[i]
   }
 }

